package biz;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import dao.FriendDAO;
import dao.QuestionDAO;
import dao.RequestDAO;
import dao.UserDAO;
import model.Friendship;
import model.IEntity;
import model.Question;
import model.User;

public class UserBIZ
{
	private String selfPhone = "";

	public boolean setSelfPhone(String pn)
	{
		if (pn.length() == 11)
		{
			selfPhone = pn;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean Register(String phoneNumber,String password)
	{
		UserDAO ud =new UserDAO();
		if(ud.addOne(new User(phoneNumber,password)))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}

	public boolean logIn(String phoneNumber, String password)
	{
		UserDAO ud = new UserDAO();
		User user = new User();
		user.setPhoneNumber(phoneNumber);
		user = (User) ud.getOne(user);
		if (user.getPassword().equals(password))
		{
			setSelfPhone(phoneNumber);
			return true;
		}
		else
		{
			return false;
		}
	}

	public List<IEntity> getAllFriends(String phoneNumber)
	{
		UserDAO ud = new UserDAO();
		FriendDAO fd = new FriendDAO();
		List<IEntity> friends = fd.getList(new Friendship(phoneNumber,null));
		return friends;
	}

	public List<IEntity> getQuestionByAsker(String phoneNumber)
	{
		QuestionDAO qd = new QuestionDAO();
		List<IEntity> questions = qd.getList(new Question(phoneNumber));
		return questions;
	}
	
	public Question getOneQuestionById(String id)
	{
		QuestionDAO qd = new QuestionDAO();
		Question q = new Question();
		q.setId(Integer.parseInt(id));
		q=(Question)qd.getOne(q);
		return q;
	}

	public List<IEntity> getFriendQuestions(String phoneNumber)
	{
		QuestionDAO qd = new QuestionDAO();
		List<IEntity> questions = qd.getFriendQuestionList(phoneNumber);
		return questions;
	}

	public List<IEntity> getFriendRequests(String phoneNumber)
	{
		RequestDAO rd = new RequestDAO();
		List<IEntity> requests = rd.getList(new Friendship(phoneNumber,null));
		return requests;
	}

	public boolean dealFriendRequest(String phoneA, String phoneB, boolean isAccept)
	{
		RequestDAO rd = new RequestDAO();
		FriendDAO fd = new FriendDAO();
		Friendship fs = (Friendship) (rd.getOne(new Friendship(phoneA, phoneB)));
		if (fs == null)
		{
			System.out.println("filalalialialaiai");
			return false;
		}
		else
		{
			if (isAccept)
			{
				fd.addOne(fs);
				System.out.println("asasasasa");
			}
			rd.deleteOne(fs);
			return true;
		}
	}

	public boolean isFriend(String phoneA, String phoneB)
	{
		FriendDAO fd = new FriendDAO();
		if (fd.getOne(new Friendship(phoneA, phoneB)) != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public boolean isRequesting(String to, String from)
	{
		RequestDAO rd = new RequestDAO();
		if (rd.getOne(new Friendship(to, from)) != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public boolean addFriendRequest(String phoneA, String phoneB)
	{
		if (isRequesting(phoneA, phoneB) || isFriend(phoneA, phoneB))
		{
			return false;
		}
		else
		{
			RequestDAO rd = new RequestDAO();
			rd.addOne(new Friendship(phoneA, phoneB));
			return true;
		}
	}

	public boolean addQuestion(Question qq)
	{
		QuestionDAO qd = new QuestionDAO();
		return qd.addOne(qq);

	}

	public JSONArray getUserListJsonByPhones(List<String> phones)
	{
		JSONArray mJsonArray = new JSONArray();
		UserDAO ud = new UserDAO();
		User user = new User();

		for (int i = 0; i < phones.size(); i++)
		{
			Map<String, String> itemMap = new HashMap<String, String>();
			user = (User) ud.getOne(new User((String) phones.get(i)));
			itemMap.put("phoneNumber", user.getPhoneNumber());
			itemMap.put("name", user.getName());
			JSONObject jsonObject = new JSONObject(itemMap);
			mJsonArray.put(jsonObject);
		}
		return mJsonArray;
	}

	public JSONArray getUserListJsonByUsers(List<IEntity> users)
	{
		JSONArray mJsonArray = new JSONArray();
		UserDAO ud = new UserDAO();
		User user = new User();

		for (int i = 0; i < users.size(); i++)
		{
			Map<String, String> itemMap = new HashMap<String, String>();
			itemMap.put("phoneNumber", ((User) users.get(i)).getPhoneNumber());
			itemMap.put("name", ((User) users.get(i)).getName());
			JSONObject jsonObject = new JSONObject(itemMap);
			mJsonArray.put(jsonObject);
		}
		return mJsonArray;
	}

	public JSONArray getHomeQuestionsJson(String userPhone)
	{
		JSONArray mJsonArray = new JSONArray();
		QuestionDAO qd = new QuestionDAO();
		List<IEntity> questions_relative = qd.getRelativeQuestions(new Question(userPhone));

		for (int i = 0; i < questions_relative.size(); i++)
		{
			Map<String, String> itemMap = new HashMap<String, String>();
			itemMap.put("id", ((Question) questions_relative.get(i)).getId()+"");
			itemMap.put("requester", ((Question) questions_relative.get(i)).getUserPhone());
			itemMap.put("title", ((Question) questions_relative.get(i)).getTitle());
			itemMap.put("details", ((Question) questions_relative.get(i)).getContent());
			JSONObject jsonObject = new JSONObject(itemMap);
			mJsonArray.put(jsonObject);
		}
		return mJsonArray;
	}
	
	
	
}
